{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%html\n",
    "<style>\n",
    ".cell-output-ipywidget-background {\n",
    "    background-color: transparent !important;\n",
    "}\n",
    ":root {\n",
    "    --jp-widgets-color: var(--vscode-editor-foreground);\n",
    "    --jp-widgets-font-size: var(--vscode-editor-font-size);\n",
    "}  \n",
    "</style>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import vllm\n",
    "\n",
    "from art.vllm import get_llm\n",
    "\n",
    "llm = await get_llm(\n",
    "    vllm.AsyncEngineArgs(\n",
    "        model=\"Qwen/Qwen3-32B\",\n",
    "        # pipeline_parallel_size=1,\n",
    "        tensor_parallel_size=torch.cuda.device_count(),\n",
    "        # data_parallel_size=1,\n",
    "        enforce_eager=True,\n",
    "        generation_config=\"vllm\",\n",
    "    ),\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import art\n",
    "from art.vllm import openai_server_task\n",
    "\n",
    "sleep_task = await openai_server_task(\n",
    "    engine=llm,\n",
    "    config=art.dev.OpenAIServerConfig(\n",
    "        log_file=\"./vllm.log\",\n",
    "        server_args=art.dev.ServerArgs(\n",
    "            api_key=\"default\",\n",
    "            return_tokens_as_token_ids=True,\n",
    "            enable_auto_tool_choice=True,\n",
    "            tool_call_parser=\"hermes\",\n",
    "        ),\n",
    "        engine_args=art.dev.EngineArgs(\n",
    "            model=llm.vllm_config.model_config.model,\n",
    "            disable_log_requests=True,\n",
    "            generation_config=\"vllm\",\n",
    "        ),\n",
    "    ),\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "await llm.sleep()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "await llm.wake_up()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import asyncio\n",
    "import os\n",
    "import signal\n",
    "\n",
    "from art.vllm import run_on_workers\n",
    "\n",
    "pids = await run_on_workers(llm, lambda: os.getpid())\n",
    "pids"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "from art.vllm import get_worker\n",
    "\n",
    "\n",
    "def sleep() -> None:\n",
    "    worker = get_worker()\n",
    "    worker.sleep()\n",
    "    os.kill(os.getpid(), signal.SIGSTOP)\n",
    "    worker.wake_up()\n",
    "\n",
    "\n",
    "sleep_task = asyncio.create_task(run_on_workers(llm, sleep))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for pid in pids:\n",
    "    os.kill(pid, signal.SIGCONT)\n",
    "await sleep_task"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "llm.shutdown()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
